home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / pc / BEERSRC.ZIP / MOUSE.C < prev    next >
Encoding:
C/C++ Source or Header  |  1991-05-10  |  4.9 KB  |  317 lines

  1. /*-------------------------------------------------------*/
  2. /*                                                         */
  3. /*                    M O U S E . C                      */
  4. /*                                                       */
  5.  
  6.  
  7.  
  8. #include "globdefs.h"
  9.  
  10. #include <stdlib.h>
  11. #include <mem.h>
  12.  
  13. #include "mouse.h"
  14. //#include "graph.h"
  15.  
  16.  
  17.  
  18. int   m_left, m_right;
  19.  
  20.  
  21. struct MOUSE {
  22.  
  23.    int    x0, y0;
  24.    int    cursor;
  25.    int    selectedcursor;
  26.    int    mode;
  27.    int    state;
  28.    void   (*extcurs)(int x, int y);
  29.  
  30. };
  31.  
  32. struct CURSOR {
  33.  
  34.    int       x, y;
  35.    unsigned  shape[M_CURSORSIZE / sizeof(int)];
  36.  
  37. } ;
  38.  
  39.  
  40. static struct MOUSE  mouse;
  41. static struct CURSOR cursor[M_MAXCURSORS] =
  42.            {
  43.               0, 0,     /* Arrow */
  44.               1,-1, 0,-1,-1,-1,-1,-1,
  45.               1,-1,-1,-1,-1,-1,-1,-1,
  46.               1,-1,-1,-1, 0,-1,-1,-1,
  47.               1,-1,-1,-1,-1,-1,-1,-1,
  48.               0, 0,     /* Smile */
  49.               0x781f, 0xe007, 0xc003, 0x8421, 0x8811, 0x0c30, 0x0c30, 0x0000,
  50.               0x0000, 0x0000, 0x0c30, 0x87e1, 0x8181, 0xc003, 0xe007, 0xf81f,
  51.               0x87e0, 0x1ff8, 0x3ffc, 0x7bde, 0x77ee, 0xf3cf, 0xf3cf, 0xffff,
  52.               0xffff, 0xffff, 0xf3cf, 0x781e, 0x7e7e, 0x3ffc, 0x1ff8, 0x07e0
  53.            };
  54.  
  55.  
  56.  
  57. void setclickmode(int mode)
  58. {
  59.  
  60.    mouse.mode = mode;
  61.    mouse.state = 0;
  62.  
  63. }
  64.  
  65.  
  66. int getclickmode(void)
  67. {
  68.  
  69.    return mouse.mode;
  70.  
  71. }
  72.  
  73.  
  74. void defexterncursor(void (*extcurs)(int x, int y))
  75. {
  76.  
  77.    mouse.extcurs = extcurs;
  78.  
  79. }
  80.  
  81.  
  82.  
  83.  
  84. static void far handler(void)
  85. {
  86.    static int    x, y, b;
  87.  
  88.  
  89.    if (mouse.cursor == M_CEXTERN) {
  90.  
  91.       m_getpos(&b, &x, &y);
  92.       (*mouse.extcurs)(x, y);
  93.       (*mouse.extcurs)(mouse.x0, mouse.y0);
  94.       mouse.x0 = x;  mouse.y0 = y;
  95.  
  96.    }
  97.  
  98. }
  99.  
  100.  
  101.  
  102.  
  103.  
  104.  
  105. void hidecursor(void)
  106. {
  107.  
  108.    if (mouse.cursor) {
  109.       if (mouse.selectedcursor == M_CEXTERN)
  110.          (*mouse.extcurs)(mouse.x0, mouse.y0);
  111.       else
  112.          m_hidecursor();
  113.  
  114.       mouse.cursor = 0;
  115.    }
  116.  
  117. }
  118.  
  119.  
  120. void showcursor(void)
  121. {
  122.    int   button;
  123.  
  124.  
  125.    if (mouse.cursor)
  126.       hidecursor();
  127.  
  128.    if (mouse.selectedcursor == M_CEXTERN && mouse.extcurs != NULL) {
  129.  
  130.       m_getpos(&button, &mouse.x0, &mouse.y0);
  131.       (*mouse.extcurs)(mouse.x0, mouse.y0);
  132.  
  133.    } else {
  134.  
  135.       m_showcursor();
  136.  
  137.    }
  138.  
  139.    mouse.cursor = mouse.selectedcursor;
  140.  
  141. }
  142.  
  143.  
  144. void selectcursor(int n)
  145. {
  146.  
  147.    int  showed;
  148.  
  149.  
  150.    if (n <= M_MAXCURSORS) {
  151.       showed = mouse.cursor;
  152.       hidecursor();
  153.       mouse.selectedcursor = n;
  154.       if (n != M_CEXTERN) {
  155.          n--;
  156.          m_definecursor(cursor[n].x, cursor[n].y,
  157.                         cursor[n].shape);
  158.       }
  159.       if (showed)
  160.          showcursor();
  161.    }
  162.  
  163. }
  164.  
  165.  
  166.  
  167. void definecursor(int n, int x, int y, byte *shape)
  168. {
  169.  
  170.    if (--n < M_MAXCURSORS) {
  171.  
  172.       cursor[n].x = x;  cursor[n].y = y;
  173.       memcpy(cursor[n].shape, shape, M_CURSORSIZE);
  174.  
  175.       if (mouse.cursor == ++n)
  176.          selectcursor(n);
  177.  
  178.    }
  179.  
  180. }
  181.  
  182.  
  183.  
  184.  
  185. void setborder(int x1, int y1, int x2, int y2)
  186. {
  187.  
  188.    m_setxborder(x1, x2);
  189.    m_setyborder(y1, y2);
  190.  
  191. }
  192.  
  193.  
  194.  
  195. void setpos(int x, int y)
  196. {
  197.  
  198.    m_setpos(x, y);
  199.    if (mouse.cursor == M_CEXTERN) {
  200.       (*mouse.extcurs)(x, y);
  201.       (*mouse.extcurs)(mouse.x0, mouse.y0);
  202.       mouse.x0 = x;  mouse.y0 = y;
  203.    }
  204.  
  205. }
  206.  
  207.  
  208.  
  209. void getpos(int *b, int *x, int *y)
  210. {
  211.  
  212.    m_getpos(b, x, y);
  213.  
  214.    if (mouse.mode == M_HOLD) {
  215.       if (*b != mouse.state) {
  216.      if (*b == 0) {
  217.         *b = mouse.state;
  218.         mouse.state = 0;
  219.      } else {
  220.         mouse.state = *b;
  221.      }
  222.       } else {
  223.          *b = 0;
  224.       }
  225.    } else {
  226.       if (*b == 0) {
  227.      *b = mouse.state;
  228.      mouse.state = 0;
  229.       } else {
  230.          mouse.state = *b;
  231.          *b = 0;
  232.       }
  233.    }
  234.  
  235. }
  236.  
  237.  
  238. void mouseclick(int *button, int *x, int *y)
  239. {
  240.  
  241.    do {
  242.       getpos(button, x, y);
  243.    } while (*button != m_left && *button != m_right);
  244.  
  245. }
  246.  
  247.  
  248. void menuclick(int *button, int *x, int *y)
  249. {
  250.  
  251.    int   tmp;
  252.  
  253.    tmp = getclickmode();
  254.    setclickmode(M_RELEASE);
  255.  
  256.    do {
  257.       getpos(button, x, y);
  258.    } while (*button != m_left && *button != m_right);
  259.  
  260.    setclickmode(tmp);
  261.  
  262. }
  263.  
  264.  
  265. void waitforrelease(int *x, int *y)
  266. {
  267.  
  268.    int   button;
  269.  
  270.    do {
  271.       m_getpos(&button, x, y);
  272.    } while (button != 0);
  273.  
  274.    mouse.state = 0;
  275.  
  276. }
  277.  
  278.  
  279. int initmouse(void)
  280. {
  281.    int   state, buttons;
  282.  
  283.  
  284.    m_deviceinit(&state, &buttons);
  285.    if (state) {
  286.       m_left = 1;  m_right = 2;
  287.  
  288.       mouse.cursor = 0;
  289.       mouse.mode = M_RELEASE;
  290.       mouse.state = 0;
  291.       mouse.extcurs = NULL;
  292.       m_seteventhdlr(handler, M_CHANGEPOS | M_PRESSLEFT | M_PRESSRIGHT);
  293.       selectcursor(M_CSMILE);
  294.  
  295.       return TRUE;
  296.  
  297.    } else {
  298.  
  299.       return FALSE;
  300.  
  301.    }
  302.  
  303. }
  304.  
  305. void clearmouse(void)
  306. {
  307.    int   state, buttons;
  308.  
  309.    m_deviceinit(&state, &buttons);
  310.    if (mouse.cursor == M_CEXTERN)
  311.       (*mouse.extcurs)(mouse.x0, mouse.y0);
  312.    mouse.cursor = 0;
  313.    mouse.mode = 0;  mouse.state = 0;
  314.  
  315. }
  316.  
  317.